<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>Introduction to singlog</title></head>
<body>
<h2>INTRODUCTION</h2>
singlog is a modern computer language derived from Prolog. Some
highlights of it features are as follows. <br>
<ul>
<li> Weakly typed<br>
<br>
You do not need to declare and specify types to any variables. <br><br>
</li>
<li> Single assignment <br>
<br>
Each variable instance can be binded to one value, and not to be
reassigned anymore (except in backtracking). <br><br>
</li>
<li> Backtracking <br>
<br>
If a step fails, the program can revert to a previous step and retry,
perhaps with a different 'bindings.' <br><br>
</li>
<li> Program itself as data <br>
<br>
singlog programs and data are like symbol trees. In interpreter mode,
you can write a program to process a program (meta-programming in AI
terms). </li>
</ul>
<br>
<br>
<h2>WAY TO PROGRAM</h2>
<h3>Basic Pattern Matching Queries</h3>
singlog programs consist of rules. Each rule is a logical fact fed to
the deduction system. Under the normal input prompt, you can input
rules directly. <br>
<br>
A rule can be multi-lined. The interpreter knows the end of a rule by
the '#' symbol at the end of a line; or when you issued an empty line
(press Enter twice) in interactive mode. <br>
<br>
Execution is triggered by a query: beginning with "?" for simple yes/no
query, or "/" for queries that need to elaborate answers. During
execution singlog system tries to "prove" the fact with all given rules
in every possible ways. yes or no is returned to indicate whether the
prove is success or not. <br>
<br>
<font face="monospace">=&gt; dog has four legs #<br>
</font><font face="monospace">=&gt; chicken has
two legs #<br>
</font><font face="monospace">=&gt; ? dog has
four legs # &nbsp; <span style="font-family: mon;"><span style="font-style: italic;">(</span></span><span style="font-style: italic;">Do a query</span>)<br>
Yes<br>
<br>
=&gt; ? dog has two legs # &nbsp; <span style="font-family: mon;"><span style="font-style: italic;">(</span></span><span style="font-style: italic;">Duery which is impossible to
prove, at this <span style="font-family: monospace;">moment</span></span>)<br>
No<br>
<br>
</font><font face="monospace">=&gt; ? list # &nbsp; <span style="font-style: italic;">(System command to list all rules</span>)<br>
dog has four legs #</font><br>
<br>
<h3>Logical Combinations</h3>
A more-complex query can be formed by comma (,) or semi-colon (;)
operators. They are the short-circuit form of logical AND/OR
respectively. Semi-colon operator enjoys higher precedence
over comma operator during parsing.<br>
<br>
<font face="monospace"><span style="font-style: italic;">(Continuing
from previous example)</span><br>
=&gt; true #<br>
</font><font face="monospace">=&gt;</font><font face="monospace">? chicken has four legs, sheep has four
legs # &nbsp; <span style="font-style: italic;">(the
latter part cannot be proved in an AND condition)</span></font><br>
<font face="monospace">No</font><br>
<font face="monospace">=&gt; ? sheep has four legs;
dog has four legs # &nbsp; <span style="font-style: italic;">(The
latter part is provable in an OR condition)</span><br>
</font><font face="monospace">Yes<br>
</font><font face="monospace">=&gt; ? dog has
four legs, (chicken has no legs; true) # &nbsp; <span style="font-style: italic;">(Getting complicated)</span><br>
</font><font face="monospace">Yes</font><br>
<h3>Using Variables</h3>
Variable names are prefixed with a dot (.). They do not require
explicit definition and are automatically recognized. Note that all
variables are local to the predicate, and they are initially in an
unbound state so that you may assign anything to it. However, only a
single assignment is allowed for each variable, which means any
re-assignment to other values will fail (returning no). <br>
<br>
You can match variable against any facts/rules. The variable would
"binded" to the matched fact. <br>
<br>
<font face="monospace">=&gt; ? .author = ywsing #
&nbsp; <span style="font-style: italic;">(Nothing but
a simple assignment)</span><br>
Yes<br>
<br>
=&gt; / dog has .var legs # &nbsp; <span style="font-style: italic;">(Match against facts)</span><br>
.var = 4<br>
1 solution<br>
<br>
=&gt; ? .num = four, .num = five # &nbsp; <span style="font-style: italic;">(No way to reassign)</span><br>
No</font><br>
<br>
Invoking a user predicate involves following actions: <br>
<ol>
<li>Initiates a search throughout the rules database.</li>
<li>For any possible match, instantiates its local variables to
unbound state.</li>
<li>Unifies parameters.</li>
<li>Tries to prove the target clause in predicate.<br>
</li>
</ol>
<br>
<h3>Backtracking, and Repeat/Fail Loops</h3>
Since there can be many facts, it is possible that a variable can be
binded to many different values, e.g.<br>
<br>
<font face="monospace">=&gt; france is in europe #<br>
</font><font face="monospace">=&gt; switzerland
is in europe #<br>
</font><font face="monospace">=&gt; norway is in
europe #<br>
</font><font face="monospace">Yes<br>
<br>
=&gt; / .country is in europe # &nbsp; <span style="font-style: italic;">(Multi-match, elaborating each
result in order)</span><br>
.country = france<br>
</font><font face="monospace">.country = switzerland<br>
</font><font face="monospace">.country = norway<br>
</font><font face="monospace">3 solutions<br>
<br>
</font>singlog takes a backtracking approach. The variable would
be binded to the first result, and continue to execute remainings of
the clause. If the sub-goal failed for whatever reason,
the states of all variables are rolled back to that previous
point. The variable, now back to unbounded state, would be re-binded to
the second result for retry. The third, fourth and even further retries
will happen if the sub-goal continues to fail. Refer to this as example.<br>
<br>
<font face="monospace">=&gt; ? .country is in europe,
&nbsp; <span style="font-style: italic;">(</span></font><font face="monospace"><span style="font-style: italic;"><span style="font-family: monospace;">Matches the variable)</span></span></font><br>
<font face="monospace">&nbsp; write .country, nl,
&nbsp;</font><font face="monospace"><span style="font-style: italic;">(<span style="font-family: monospace;">Writes the country name and
output a next line character)</span></span></font><br>
<font face="monospace">&nbsp; fail # &nbsp; <span style="font-style: italic;">(<span style="font-family: monospace;">Deliberately fails to cause
backtrack)</span></span><br>
</font><font face="monospace">france<br>
</font><font face="monospace">switzerland<br>
</font><font face="monospace">normway<br>
No<br>
<br>
</font>With this backtracking ability, we may form an infinite
loop using repeat/fail. repeat is a system predicate that provides
infinite retry, while fail would turn down all of them, and the program
would keep on looping in goals between.<br>
<br>
<font face="monospace">=&gt; ? repeat, write
'brabrabra', nl, fail # &nbsp; <span style="font-style: italic;">(</span></font><font face="monospace"><span style="font-style: italic;"><span style="font-family: monospace;">Matches the variable)</span></span></font><br>
<font face="monospace">brabrabra<br>
</font><font face="monospace">brabrabra<br>
</font><font face="monospace">brabrabra<br>
&nbsp; &nbsp;:<br>
&nbsp; &nbsp;:<br>
</font>
<h3>Inference Rules</h3>
g<br>
<br>
<span style="font-family: monospace;">(Continued from the
multi-matching example)<br>
=&gt; Salary Mary 1000 #<br>
</span><span style="font-family: monospace;">=&gt;
Salary John 1500 #<br>
</span><span style="font-family: monospace;">=&gt;
Salary Boss 10000 #</span><br>
<span style="font-family: monospace;">=</span><font face="monospace">&gt; Wealthy .person :- Salary
.person .salary, .salary &gt; 2000 # &nbsp; <span style="font-style: italic;">(</span></font><font face="monospace"><span style="font-style: italic;"><span style="font-family: monospace;">Matches the variable)</span></span></font><br>
<h3>Using Cuts to Avoid Backtracking</h3>
Cuts are represented by "!" symbols. This symbol is an operator here.
It removes all choicepoints for backtracking, from that point way back
to the point of invocation of the local predicate. <br>
<br>
<h2>DATA STRUCTURE</h2>
There are three data types. They are: <br>
<ul>
<li>Atom, the simplest data type, which can be one of<br><br>
</li>
<ul>
<li>Identifier, e.g.<br>
abc, dog, .varName<br>
a.very.long.identifier<br>
'an identifier with special characters can be single-quoted'</li>
<li>String, e.g.<br>
"Hello logical world"</li>
<li>Number. Only integers are supported currently,
which should be equivalent to a C++ int in the platform the interpreter
being compiled. e.g.<br>
0, 1, 2<br>
3000, 2147483647<br>
-1,&nbsp;-2147483648<br><br>
</li>
</ul>
<li>Tree<br><br>
A construct with an operator (1 or 2 characters) and two leafs. Each
leaf is can be an atom, a tree or a tuple. Any expressions are
represented as tree, e.g. 99-88, 1+2*3.<br><br>
</li>
<li>Tuple<br><br>
A list of many data separated by spaces. Each data can be an atom, a
tree or a tuple. Note that the space has lowest precendence among all
operators and you need parentheses around a tuple often. e.g.<br>
1 2 3<br>
(Caro mio ben)<br>
(A tuple with (A sub tuple))</li>
</ul>
<h2><br>
</h2>
<h2>BUILT-IN PREDICATES</h2>
...<br>
<br>
<h2>NAMING CONVENTIONS</h2>
...<br>
<br>
<h2>AUTHOR</h2>
<br>
ywsing &lt;stupidsing (a) gmail dot com&gt;<br>
</body></html>